/**
 * Created by Administrator on 2018/10/17 0017.
 */
//获取分页信息
function getRoleListPage(pageurl,keyword,curr){
    $("#allChk").prop("checked",false);
    sign.params={
        keyword:keyword,
        curr:curr
    };
    var encode_post = sign.encode_post();
    layui.use(['laypage','form', 'layer'], function(){
        var laypage = layui.laypage
            ,layer = layui.layer
            ,form = layui.form;
        var index = layer.load(2, {shade: [0.1,'#fff'],time: 60*1000});

        $.post(pageurl,encode_post, function(data){
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }

            datass = sign.decode_post(data);

            //选择
            role_list(datass);

            form.render('checkbox');
            layer.close(index);
            if(datass.data.page<=1){
                $('#pages').html('');
            }
            else {
                //显示分页
                laypage.render({
                    elem: 'pages', 			// 容器
                    count: datass.data.total,     	// 总页数(后台的)
                    curr: datass.data.curr, 		//当前页(后台获取到的)
                    prev: '&laquo;',
                    next: '&raquo;',
                    jump: function (obj, first) {
                        if (!first) {
                            getListPage(pageurl,keyword,obj.curr);
                        }
                    }
                });
            }
        });
    });
}
//获取替换信息
function role_list(data) {
    var html='';
    if (data['data']['list'] === undefined || data['data']['list'].length == 0) {
        html ='<tr><td align="center" colspan="12">暂无相关数据</td></tr>';
        //页面数据替换
        $('#role_list').html(html);
    }
    else {
        list = data['data']['list'];
        for (var i in list) {
            list[i].role_id = list[i].role_id ? list[i].role_id : '';
            list[i].role_name = list[i].role_name ? list[i].role_name : '';
            list[i].name = list[i].name ? list[i].name : '';
            list[i].role_auth_ac = list[i].role_auth_ac ? list[i].role_auth_ac : '';

            html += '<tr> <td class="tc"><input name="id[]" value="' + list[i].role_id + '" type="checkbox"></td> <td>';
            html += list[i].role_name + '</td>';
            html += '<td>' + list[i].name + '</td>';

            html += '<td>'+list[i].role_auth_ac+'</td><td class="link-update">';
            html += '<a class="link-update" href="#" onclick="oncli(\''+list[i].role_id+'\',\''+list[i].role_name+'\')">';
            html += '修改</a>';
            html += '<a class="link-del" href="#" onclick="role_del(' + list[i].role_id + ')">删除</a>';
            html += '</td></tr>';
        }
        $('#role_list').html(html);
    }
}



//修改弹出层角色信息
function oncli(id,name) {
    document.getElementById('role_id').value=id;
    document.getElementById('role_name').value=name;
    $('.bgPop,.pop').show();
}
function outcli() {
    $('.bgPop,.pop').hide();
}
function role_update() {
    var checkedNum = $("input[name='checkid']:checked").length;
    if (checkedNum == 0) {
        layer.msg('请选择至少一项', {icon: 2});
        return;
    }
    // 批量选择
    var checkedList = new Array();
    $("input[name='checkid']:checked").each(function () {
        checkedList.push($(this).val());
    });

    var check_id =checkedList.join(',');
    var role_name = $('#role_name').val();
    var role_id = $('#role_id').val();

    console.log(role_id);
    sign.params={
        role_id :role_id,
        role_name:role_name,
        role_auth_ids:check_id
    };
    var encode_post = sign.encode_post();
    $.post(url_list.role_update,encode_post,function (data) {
        //解码
        if(typeof data == 'string'){
            data = eval('('+data+')');
        }
        data = sign.decode_post(data);

        if(data.code==200){
            layer.msg(data.msg, {icon: 1},1500);
            getRoleListPage(url_list.role_list,keyword);outcli();
        }else {
            layer.msg(data.msg, {icon: 2},1500);
        }
    })
}

//添加角色弹窗
function role_add(){
    //弹窗
    layer.prompt({
        formType: 2,
        title: '请输入角色名称',
        area: ['400px', '50px'] //自定义文本域宽高
    }, function(value, index, reply_content){
        sign.params={
            role_name:value
        };
        var encode_data = sign.encode_post();
        $.post(url_list.role_add,encode_data,function (data) {
            //解码
            if(typeof data == 'string'){
                data = JSON.parse(data);
            }
            data = sign.decode_post(data);
            console.log(data);
            if (data.code == 200) {
                layer.msg(data.msg, {icon: 1, time: 1500});
                getRoleListPage(url_list.role_list,keyword);
            } else {
                layer.msg(data.msg, {icon: 2});
            }
        });
        layer.close(index);
    });
}


//删除
function role_del(id) {
    sign.params={
        role_id:id,
        del:0
    };
    var encode_post = sign.encode_post();
    if (confirm("确定要删除？")) {
        $.post(url_list.role_update, encode_post, function (data) {
            if (typeof data == 'string') {
                data = JSON.parse(data);
            }
            data = sign.decode_post(data);
            console.log(data);
            if (data.code == 200) {
                layer.msg(data.msg, {icon: 1}, 1500);
                getRoleListPage(url_list.role_list, keyword);
            } else {
                layer.msg(data.msg, {icon: 2}, 1500);
            }

        });
    }
}
//批量删除
function role_dels(type) {
    var checkedNum = $("input[name='id[]']:checked").length;
    if (checkedNum == 0) {
        layer.msg('请选择至少一项', {icon: 2});
        return;
    }
    // 批量选择
    if (confirm("确定要删除？")) {
        var checkedList = new Array();

        $("input[name='id[]']:checked").each(function () {
            checkedList.push($(this).val());
        });
    }
    var check_id =checkedList.join(',');
    sign.params={
        role_id :check_id,
        del:0
    };
    var encode_post = sign.encode_post();
    $.post(url_list.role_update,encode_post,function (data) {
        if (typeof data == 'string') {
            data = JSON.parse(data);
        }
        data = sign.decode_post(data);
        console.log(data);
        if (data.code == 200) {
            layer.msg(data.msg, {icon: 1}, 1500);
            getRoleListPage(url_list.role_list, keyword);
        } else {
            layer.msg(data.msg, {icon: 2}, 1500);
        }
    })

}

//全选
$("#allChk").click(function() {
    $("input[name='id[]']").prop("checked",this.checked);
});

